package com.anlogic.sdk.ui.tests.utils;

import java.util.regex.Pattern;

import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.waits.ICondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;

import com.anlogic.sdk.ui.tests.utils.SF1Utils.ConsoleTextMatches;

public class BootImageUtils {
	
	public static void CreatebootImage(SWTWorkbenchBot bot,String chip,String outputFormat,String fillBytes,boolean QspiX1Mode,boolean SDEMMC
			,String AuthenticationType,boolean BHAuth,boolean AuthKeys,boolean GenerateSpkSignature) {
		
		bot.menu("Tools").menu("Create Boot Image").click();
		bot.comboBox(0).setSelection(chip);
		bot.radio("Create new BIF file").click();
		//bot.radio("Import from existing BIF file").click();
		bot.radio("Create new BIF file").click();
		bot.tabItem("Security").activate();
		bot.tabItem("Basic").activate();
		bot.textWithLabel("Output BIF file path: ").setText("D:\\AnlogicSDK\\resources\\boot.bif");
		bot.comboBox(1).setSelection(outputFormat);
		bot.textWithLabel("Output path: ").setText("D:\\AnlogicSDK\\resources\\BOOT.bin");
		bot.textWithLabel("Fill Bytes: ").setText(fillBytes);
		
		if (QspiX1Mode) {
			SWTBotCheckBox xipBox = bot.checkBox("Use Qspi X1 Mode");
			bot.waitUntil(Conditions.widgetIsEnabled(xipBox), 10_000);
			if (!xipBox.isChecked()) {
				xipBox.click();
			}
		}
		if (SDEMMC) {
			SWTBotCheckBox xipBox = bot.checkBox("Use SD/EMMC 400KHz");
			bot.waitUntil(Conditions.widgetIsEnabled(xipBox), 10_000);
			if (!xipBox.isChecked()) {
				xipBox.click();
			}
		}
		
		
		bot.tabItem("Security").activate();
		
		bot.tabItem("Authentication").activate();
		

		bot.textWithLabel("PPK: ").setText("D:\\AnlogicSDK\\resources\\ppkfile.pem");
		bot.textWithLabel("PSK: ").setText("D:\\AnlogicSDK\\resources\\pskfile.pem");
		bot.textWithLabel("SPK: ").setText("D:\\AnlogicSDK\\resources\\spkfile.pem");
		
		bot.textWithLabel("SSK: ").setText("D:\\AnlogicSDK\\resources\\sskfile.pem");
		
		bot.textWithLabel("SPK Signature: ").setText("D:\\AnlogicSDK\\resources\\spksignfile.sig");
	
		bot.comboBox(1).setSelection(AuthenticationType);
		bot.textWithLabel("SPK ID: ").setText("");
		
		if (BHAuth) {
			SWTBotCheckBox xipBox = bot.checkBox("Use BH Auth");
			bot.waitUntil(Conditions.widgetIsEnabled(xipBox), 10_000);
			if (!xipBox.isChecked()) {
				xipBox.click();
			}
		}
		if (AuthKeys) {
			SWTBotCheckBox xipBox = bot.checkBox("Generate Auth Keys");
			bot.waitUntil(Conditions.widgetIsEnabled(xipBox), 10_000);
			if (!xipBox.isChecked()) {
				xipBox.click();
			}
		}
		if (AuthKeys) {
			SWTBotCheckBox xipBox = bot.checkBox("Generate Spk Signature");
			bot.waitUntil(Conditions.widgetIsEnabled(xipBox), 10_000);
			if (!xipBox.isChecked()) {
				xipBox.click();
			}
		}
		
		
		bot.tabItem("Encryption").activate();
		bot.textWithLabel("Key file: ").setText("D:\\AnlogicSDK\\resources\\sm4keyfile.nky");
		bot.comboBox(1).setSelection("EFUSE RED");
		bot.comboBox(2).setSelection("sm4");
		bot.tabItem("Authentication").activate();
		bot.button("Add").click();
		bot.textWithLabel("File path: ").setText("D:\\AnlogicSDK\\resources1\\fsbl.bin");
		bot.comboBox(0).setSelection("bootloader");
		bot.comboBox(1).setSelection("PS");
		bot.comboBox(2).setSelection("A35_0");
		bot.comboBox(3).setSelection("none");
		bot.comboBox(4).setSelection("sm4");
		bot.comboBox(5).setSelection("none");
		bot.comboBox(6).setSelection("none");
		bot.textWithLabel("Load: ").setText("0x3000000");
		bot.textWithLabel("Alignment: ").setText("");
		bot.textWithLabel("Offset: ").setText("");
		bot.textWithLabel("Execution: ").setText("");
		
		bot.button("OK").click();
		bot.button("Add").click();
		bot.textWithLabel("File path: ").setText("D:\\AnlogicSDK\\resources1\\gpio_led.bin");
		bot.comboBox().setSelection("datafile");
		bot.comboBox(1).setSelection("PL");
		bot.comboBox(3).setSelection("none");
		bot.comboBox(4).setSelection("sm4");
		bot.comboBox(5).setSelection("sha2");
		bot.comboBox(6).setSelection("fsbl");
		bot.textWithLabel("Alignment: ").setText("");
		bot.textWithLabel("Offset: ").setText("");
		bot.textWithLabel("Execution: ").setText("");
		bot.textWithLabel("Load: ").setText("");
		bot.button("OK").click();
		bot.table().select("D:\\AnlogicSDK\\resources1\\gpio_led.bin");
		bot.table().getTableItem("D:\\AnlogicSDK\\resources1\\gpio_led.bin").doubleClick();
		
		bot.button("OK").click();
		bot.button("Create Image").click();
		bot.button("OK").click();
		
		SWTBotView consoleView = bot.viewByPartName("Console");
		consoleView.setFocus();
		if (consoleView.isActive()) {
			System.out.println(111);
			Pattern p = Pattern.compile(".*Bootimage generated successfully!.*", Pattern.DOTALL);
			//Thread.sleep(000);
			bot.waitUntil(consoleTextMatches(consoleView, p),60 * 1000);
		}


	}
	
	 protected static ICondition consoleTextMatches(SWTBotView consoleView, Pattern pattern) {
			return new ConsoleTextMatches(consoleView, pattern);
		}

		static class ConsoleTextMatches implements ICondition {
			private final SWTBotView view;
			private Pattern pattern;

			public ConsoleTextMatches(SWTBotView view, Pattern pattern) {
				this.view = view;
				this.pattern = pattern;
			}

			@Override
			public boolean test() {
				if (view.isActive()) {
					String output = view.bot().styledText().getText();
					java.util.regex.Matcher m = pattern.matcher(output);
					return m.matches();
				}
				return false;
			}

			@Override
			public void init(SWTBot bot) {
			}

			@Override
			public String getFailureMessage() {
				return null;
			}
		}

}
